#include "StdAfx.h"
#include "IOCPDataProcessor.h"
#include <sstream>

CIOCPDataProcessor::CIOCPDataProcessor()
{
}

CIOCPDataProcessor::~CIOCPDataProcessor(void)
{
	Close();
}

bool CIOCPDataProcessor::Create()
{
	m_nCount = 0;
	return true;
}

void CIOCPDataProcessor::Close()
{
}
bool CIOCPDataProcessor::HandleData(CIOCPAsynClientSocket* _pClient, char* _cArrBuffer, int _nLen)
{
	//printf("handle data len: %d\n",_nLen);
	CIOCPRecvDataPackage* pPackage = NULL;


	std::map<CIOCPAsynClientSocket*,CIOCPRecvDataPackage*>::iterator  iter = m_mapConnectionPackage.find(_pClient);
	if (iter != m_mapConnectionPackage.end())
	{
		pPackage = iter->second;
	}else{
		pPackage = new CIOCPRecvDataPackage;
		m_mapConnectionPackage[_pClient] = pPackage;
	}
	
	int nPackage = 0;
	while((_nLen - nPackage) > 0){
		nPackage += pPackage->Package(_cArrBuffer + nPackage,_nLen - nPackage);
		//printf("package data len %d\n",nPackage);
		if (pPackage->IsPackaged())
		{
			Process(pPackage->GetPackageData(),pPackage->GetPackageLen());
			pPackage->Reset();
		}
	}
	
	return true;
}
